<?php
/**
 * Created by PhpStorm.
 * User: nhson219
 * Date: 9/8/14
 * Time: 9:02 AM
 */
?>

<div class="poper-container" style="display: none">

    <ul id="list-notifications" class="list-notifications">
        <div id="loading-image loading-notifications"></div>
        <li style="display: none" id="notifications-no-data">Không có dữ liệu</li>

    </ul>
    <ul id="list-notifications-more" class="list-notifications">

    </ul>
    <div id="notifications-more" style="float: left;display: none"><a onclick="moreNotifications()" style="cursor: pointer;">Xem thêm</a></div>
</div>
<script>
    var track = 0;
    $(document).ready(function(){
        $('.danger').popover({
            html : true,
            placement : 'bottom',
            content: function() {
                return $('.poper-container').html();
            },
            template: '<div class="popover"><div class="arrow"></div><div class="popover-inner"><div class="popover-content"><p></p></div></div></div>'
        }).on('click',function(e){
            e.stopPropagation();
        }).on('hide.bs.popover',function(){
            track = 0;
        });
//        $('html').click(function(){
//           $('.danger').popover('hide');
//        });
        getMessages();
    });

    function getMessages(){
        $.ajax({
            type: "POST",
            url: "<?php echo base_url()."index.php/messages/getMessages"?>",
            data : "track=0",
            beforeSend: function () {
                $('#loading-notifications').show();
            },
            success: function (data) {
                $('#loading-notifications').hide();
                data = $.parseJSON(data);
                if ((data.status == 'error') || (data.status == 'no-data')) {
                    $('#notifications-no-data').show();
                } else {
                    $("#notifications-more").show();
                    var cList = $('#list-notifications');
                    cList.empty();
                    $.each(data.data, function (i, item) {

                        //data.data[i] = reserve(data.data[i]);
                        if(data.data[i].logo != null)
                            var url_img = BASE_URL+"files/logos/"+data.data[i].logo;
                        else
                            var url_img = BASE_URL+"app/css/images/default-images.jpg";

                        var url_project = BASE_URL+"index.php/project/view/"+data.data[i].id;

                        switch (data.data[i].type){
                            case "1":
                                var messages = "<div><strong>"+data.data[i].user_name+"</strong>"
                                    +" <?php echo $this->lang->line('bid');?> "
                                    +"$"+data.data[i].bid_amount+" <?php echo $this->lang->line('on')?> "
                                    +"<strong>"+data.data[i].project_name+"</strong></div>";
                                break;
                            case "2":
                                var messages = "<div><strong>"+data.data[i].user_name+"</strong>"
                                    +" <?php echo $this->lang->line('accept bid');?> "
                                    +"$"+data.data[i].bid_amount+" <?php echo $this->lang->line('on')?> "
                                    +"<strong>"+data.data[i].project_name+"</strong></div>";
                                break;
                            case "3":
                                var messages = "<div><strong>"+data.data[i].user_name+"</strong>"
                                    +" <?php echo $this->lang->line('invite project');?> "
                                    +"<strong>"+data.data[i].project_name+"</strong></div>";
                                break;
                            default:
                                var message = "";
                                break;
                        }

                        var li = $('<li>').html("<a href='"+url_project+"'>"+
                            "<figure><img class='img-responsive' src='"+url_img+"'/>"+"</figure>"+messages+"</a>").appendTo(cList);
                    });
                }
                setTimeout(function(){getMessages();}, 10000);
            }
        });
    }
    function moreNotifications(){
        track += 5;
        $.ajax({
            type: "POST",
            url: "<?php echo base_url()."index.php/messages/moreNotifications"?>",
            data : "track="+track,
            beforeSend: function () {
                $('#loading-notifications').show();
            },
            success: function (data) {
                $('#loading-notifications').hide();
                data = $.parseJSON(data);
                if ((data.status == 'error') || (data.status == 'no-data')) {
                    $('#notifications-no-data').show();
                } else {
                    //$("#notifications-more").show();
                    var cList_more = $('#list-notifications-more');
                    //cList.empty();
                    $.each(data.data, function (i, item) {
                        //data.data[i] = reserve(data.data[i]);
                        if(data.data[i].logo != null)
                            var url_img = BASE_URL+"files/logos/"+data.data[i].logo;
                        else
                            var url_img = BASE_URL+"app/css/images/default-images.jpg";

                        var url_project = BASE_URL+"index.php/project/view/"+data.data[i].id;

                        switch (data.data[i].type){
                            case "1":
                                var messages = "<div><strong>"+data.data[i].user_name+"</strong>"
                                    +" <?php echo $this->lang->line('bid');?> "
                                    +"$"+data.data[i].bid_amount+" <?php echo $this->lang->line('on')?> "
                                    +"<strong>"+data.data[i].project_name+"</strong></div>";
                                break;
                            case "2":
                                var messages = "<div><strong>"+data.data[i].user_name+"</strong>"
                                    +" <?php echo $this->lang->line('accept bid');?> "
                                    +"$"+data.data[i].bid_amount+" <?php echo $this->lang->line('on')?> "
                                    +"<strong>"+data.data[i].project_name+"</strong></div>";
                                break;
                            default:
                                var message = "";
                                break;
                        }

                        var li = $('<li>').html("<a href='"+url_project+"'>"+
                            "<figure><img class='img-responsive' src='"+url_img+"'/>"+"</figure>"+messages+"</a>").appendTo(cList_more);
                    });
                }
                //setTimeout(function(){getMessages();}, 10000);
            }
        });
    }
</script>